
import DataAccess.KetNoiDuLieu;
import Model.SinhVien;
import hocvien.FrmThemHocVien;
import java.awt.Component;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.prompt.PromptSupport;
import org.sql2o.Connection;
import thongtin.FrmLop;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author 102lptop
 */
public class TimKiemHocSinh extends javax.swing.JFrame {

    /**
     * Creates new form TimKiemHocSinh
     */
    public TimKiemHocSinh() {
        initComponents();
        TaiDulieuForm();
    }

    private void TaiDulieuForm() {
        PromptSupport.setPrompt("A001, B002,...", txtMaSV);
        PromptSupport.setPrompt("Tên", txtHoTenSV);

        this.setExtendedState(MAXIMIZED_BOTH);
        rootPane.setDefaultButton(btnTimKiem);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btgGioiTinh = new javax.swing.ButtonGroup();
        pnChucNangChinh = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pnXuLy = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtHoTenSV = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtMaSV = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtNganh = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnTimKiem = new javax.swing.JButton();
        txtNgaySinh = new com.toedter.calendar.JDateChooser();
        cbLop = new javax.swing.JComboBox();
        cbMaKhoa1 = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        btnThemHocVien = new javax.swing.JButton();
        rbGioiTinhAll = new javax.swing.JRadioButton();
        rbNu = new javax.swing.JRadioButton();
        rbNam = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Tìm kiếm học viên");

        pnXuLy.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin")));
        pnXuLy.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Họ tên SV:");
        pnXuLy.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 30, -1, -1));
        pnXuLy.add(txtHoTenSV, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 30, 189, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Ngày Sinh");
        pnXuLy.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setText("Lớp");
        pnXuLy.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 150, -1, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("Mã khoa:");
        pnXuLy.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 70, 66, -1));
        pnXuLy.add(txtMaSV, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 30, 157, -1));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("Giới Tính");
        pnXuLy.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, -1));
        pnXuLy.add(txtNganh, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 110, 189, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Nhập Mã SV");
        pnXuLy.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

        btnTimKiem.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnTimKiem.setText("Tìm kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });
        pnXuLy.add(btnTimKiem, new org.netbeans.lib.awtextra.AbsoluteConstraints(580, 30, 85, 40));
        pnXuLy.add(txtNgaySinh, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 70, 157, -1));

        pnXuLy.add(cbLop, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 150, 190, -1));

        pnXuLy.add(cbMaKhoa1, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 70, 190, -1));

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel10.setText("Ngành");
        pnXuLy.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 110, -1, -1));

        btnThemHocVien.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnThemHocVien.setText("Thêm");
        btnThemHocVien.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemHocVienActionPerformed(evt);
            }
        });
        pnXuLy.add(btnThemHocVien, new org.netbeans.lib.awtextra.AbsoluteConstraints(580, 90, 85, 40));

        btgGioiTinh.add(rbGioiTinhAll);
        rbGioiTinhAll.setText("Tất cả");
        pnXuLy.add(rbGioiTinhAll, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 110, -1, -1));

        btgGioiTinh.add(rbNu);
        rbNu.setText("Nữ");
        pnXuLy.add(rbNu, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 170, -1, -1));

        btgGioiTinh.add(rbNam);
        rbNam.setText("Nam");
        pnXuLy.add(rbNam, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 140, -1, -1));

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Mã SV", "Họ tên SV", "Ngày sinh", "Giới tính", "Khóa học", "Lớp", "Ngành"
            }
        ));
        jTable2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout pnChucNangChinhLayout = new javax.swing.GroupLayout(pnChucNangChinh);
        pnChucNangChinh.setLayout(pnChucNangChinhLayout);
        pnChucNangChinhLayout.setHorizontalGroup(
            pnChucNangChinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
            .addGroup(pnChucNangChinhLayout.createSequentialGroup()
                .addGroup(pnChucNangChinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnChucNangChinhLayout.createSequentialGroup()
                        .addGap(186, 186, 186)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 443, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnChucNangChinhLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(pnXuLy, javax.swing.GroupLayout.PREFERRED_SIZE, 674, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(187, Short.MAX_VALUE))
        );
        pnChucNangChinhLayout.setVerticalGroup(
            pnChucNangChinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnChucNangChinhLayout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnXuLy, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                .addGap(19, 19, 19))
        );

        getContentPane().add(pnChucNangChinh);

        jMenu1.setText("Chương trình");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Lớp học");

        jMenuItem1.setText("Thêm lớp học");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        try {
            // TODO add your handling code here:
            String sql = "EXECUTE uspLaySinhVien @MaSV = :MaSV, @TenSV = :TenSV,"
                    + " @NgaySinh = :NgaySinh, @KhoaHoc = :KhoaHoc, @GioiTinh = :GioiTinh,"
                    + " @Lop = :Lop, @Nganh = :Nganh";

            String gioiTinh = "";

            if (rbNam.isSelected()) {
                gioiTinh = "0";
            } else if (rbNam.isSelected()) {
                gioiTinh = "1";
            }

            Connection ketNoi = KetNoiDuLieu.KetNoi();

            List<SinhVien> sinhViens = ketNoi.createQuery(sql)
                    .addParameter("MaSV", txtMaSV.getText())
                    .addParameter("TenSV", txtHoTenSV.getText())
                    .addParameter("NgaySinh", txtNgaySinh.getDate())
                    .addParameter("KhoaHoc", ((ComboItem) cbLop.getSelectedItem()).getValue())
                    .addParameter("GioiTinh", gioiTinh)
                    .addParameter("Lop", ((ComboItem) cbLop.getSelectedItem()).getValue())
                    .addParameter("Nganh", txtNganh.getText())
                    .executeAndFetch(SinhVien.class);

            // Đổ dữ liệu vào bảng
            // Lấy mẫu các cột
            DefaultTableModel tbModel = (DefaultTableModel) jTable2.getModel();

            // Xóa nội dung cũ của bảng
            tbModel.setRowCount(0);

            // Thêm dòng dữ liệu
            for (int i = 0; i < sinhViens.size(); i++) {
                SinhVien sv = sinhViens.get(i);
                String[] data = new String[8];
                data[0] = i + "";
                data[1] = sv.getMaSV();
                data[2] = sv.getTenSV();
                data[3] = sv.getNgaySinh().toString();
                data[4] = sv.getGioiTinh();
                data[5] = sv.getKhoaHoc();
                data[6] = sv.getLop();
                data[7] = sv.getNgành();
                tbModel.addRow(data);
            }

            // Cập nhập hiển thị
            jTable2.setModel(tbModel);
        } catch (ClassNotFoundException | SQLException ex) {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage());
        }
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void btnThemHocVienActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemHocVienActionPerformed
        HienCuaSoNhapDuLieu(new FrmThemHocVien(), "Thêm học viên mới");
    }//GEN-LAST:event_btnThemHocVienActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        HienCuaSoNhapDuLieu(new FrmLop(), "Thêm lớp học mới");
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
     * @param panel JPanel có các trường nhập nội dung
     * @param title Tiêu đề của cửa sổ
     */
    private void HienCuaSoNhapDuLieu(Component panel, String title) {
        JDialog jd = new JDialog();
        jd.add(panel);
        jd.setTitle(title);
        jd.setLayout(new java.awt.FlowLayout());

        // Thiết đặt cửa sổ luôn nằm trên cùng
        jd.setModal(true);
        jd.setResizable(false);
        jd.setSize(new java.awt.Dimension(600, 600));

        // Căn chính giữa màn hình
        jd.setLocationRelativeTo(null);
        // Hiện cửa sổ
        jd.setVisible(true);
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmThemHocVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new TimKiemHocSinh().setVisible(true);
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btgGioiTinh;
    private javax.swing.JButton btnThemHocVien;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JComboBox cbLop;
    private javax.swing.JComboBox cbMaKhoa1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JPanel pnChucNangChinh;
    private javax.swing.JPanel pnXuLy;
    private javax.swing.JRadioButton rbGioiTinhAll;
    private javax.swing.JRadioButton rbNam;
    private javax.swing.JRadioButton rbNu;
    private javax.swing.JTextField txtHoTenSV;
    private javax.swing.JTextField txtMaSV;
    private javax.swing.JTextField txtNganh;
    private com.toedter.calendar.JDateChooser txtNgaySinh;
    // End of variables declaration//GEN-END:variables

    private static class ComboItem {

        private final String text;
        private final String value;

        public ComboItem(String text, String value) {
            this.text = text;
            this.value = value;
        }

        @Override
        public String toString() {
            return text;
        }

        public String getText() {
            return text;
        }

        public String getValue() {
            return value;
        }
    }
}
